<template>
	<view class="tb-privacy" :class="[{'tb-bottom':position=='bottom'}]" v-if="showPrivacy">
		<view class="tb-privacy-container" :style="{'--color':color,'--bgcolor':bgcolor}">
			<view class="tb-title">
				用户隐私保护提示
				
			</view>
			<view class="tb-content">
				使用前请仔细阅读
				<text class="text-ngreen" @click.stop="handleOpenPrivacyContract">{{ privacyContractName }}</text>。
				当您点击同意后，即表示您已理解并同意该条款内容，该条款将对您产生法律约束力。如您拒绝，将无法使用该服务。
			</view>
			<view class="tb-footer">
				<view class="btn disagree-btn" @click="handleRefuse">拒绝</view>
				<button id="agree-btn" class="btn agree-btn" open-type="agreePrivacyAuthorization"
					@agreeprivacyauthorization="handleAgree">
					同意
				</button>
			</view>
		</view>
	</view>
</template>
<script>
	import { hideLoading } from '@/utils/helper';
	export default {
		name: "tb-privacy",
		emits: ['agree', 'disagree'],
		props: {
			position: {
				type: String,
				default:'center'
			},
			color: {
				type: String,
				default: '#0396FF'
			},
			bgcolor: {
				type: String,
				default: '#ffffff'
			},
			onNeed: {
				type: Boolean,
				default: true
			},
			hideTabBar: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				resolvePrivacyAuthorization: null,
				showPrivacy: false,
				privacyContractName: "", // 小程序协议名称
			};
		},
		methods: {
			open(name) {
				if (this.hideTabBar) {
					uni.hideTabBar({
						success: res => {
							// console.log("hideTabBar", res);
						},
						fail: err => {
							// console.error("hideTabBar", err);
						},
					});
				}
				this.privacyContractName = name
				this.showPrivacy = true;
				hideLoading()
				
			},
			close() {
				this.showPrivacy = false;
				if (this.hideTabBar) {
					uni.showTabBar({
						success: res => {
							// console.log("showTabBar", res);
						},
						fail: err => {
							// console.error("showTabBar", err);
						},
					});
				}
			},
			// 点击同意
			handleAgree() {
				// 需要用户同意隐私授权时
				if (this.onNeed) {
					this.resolvePrivacyAuthorization({
						buttonId: "agree-btn",
						event: "agree",
					});
				}
				this.close();
				this.$emit('agree')
			},
			// 点击取消
			handleRefuse() {
				if (this.onNeed) {
					this.resolvePrivacyAuthorization({event: "disagree",});
				}
				this.close();
				this.$emit('disagree')
			},
			// 查看隐私协议内容
			handleOpenPrivacyContract() {
				uni.openPrivacyContract({
					success: res => {
						// console.log("openPrivacyContract success", res);
					},
					fail: err => {
						// console.error("openPrivacyContract fail", err);
					},
				});
			},
			// 进入时获取隐私是否需要弹出隐私协议
			checkPrivacySetting() {
				if(uni.getPrivacySetting){
					uni.getPrivacySetting({
						success: res => {
							console.log('getPrivacySetting', res,this.onNeed);
							// 如果是needAuthorization为false，无需弹出隐私协议
							if (res.needAuthorization === false) {
								this.showPrivacy?this.showPrivacy = false:''
								return;
							}
							if (this.onNeed) {
								uni.onNeedPrivacyAuthorization(resolve => {
									this.open(res.privacyContractName)
									console.log('onNeedPrivacyAuthorization',resolve)
									this.resolvePrivacyAuthorization = resolve;
								});
							} else {
								this.open(res.privacyContractName)
							}
							
						},
						fail: () => {},
						complete: () => {},
					});
				}
				
			},
		},
		mounted() {
			this.checkPrivacySetting();
		},
	};
</script>

<style lang="scss">
	.tb-privacy {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 99999;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		box-sizing: border-box;
		align-items: center;
		justify-content: center;
		-webkit-justify-content: center;
		animation: fadeIn 0.2s linear both;
	}


	.tb-privacy-container {
		width: 580rpx;
		min-height: 400rpx;
		background: var(--bgcolor);
		border-radius: 16px;
		padding: 50rpx;
		font-size: 14px;
		animation: fadeInBig 0.2s 0.2s linear both;
		backdrop-filter: blur(10rpx); //毛玻璃属性

		.tb-title {
			color: #333333;
			font-size: 36rpx;
			text-align: center;
		}

		.tb-content {
			color: #595959;
			margin-top: 36rpx;
			margin-bottom: 36rpx;
			line-height: 50rpx;

			// text {
			// 	color: var(--color);
			// }
		}

		.tb-footer {
			display: flex;
			justify-content: space-between;

			// 重置微信小程序的按钮样式
			.agree-btn:after {
				border: none;
			}

			.btn {
				width: 200rpx;
				line-height: 80rpx;
				border-radius: 24px;
				text-align: center;
			}

			.disagree-btn {
				background-color: #f6f6f6;
				color: #4c4c4c;
			}

			.agree-btn {
				line-height: 80rpx;
				background-color: #56BF7A;//var(--color)
				color: #fff;
				margin: 0;
			}
		}
	}

	.tb-bottom {
		align-items: flex-end;

		.tb-privacy-container {
			width: 100%;
			animation: fadeIn 0.2s linear both;
			animation: fadeInUp 0.2s 0.2s linear both;
			padding-bottom: calc(env(safe-area-inset-bottom) + 30rpx);
			border-radius: 24px 24px 0 0;
		}

		.footer {
			padding: 0 40rpx;

			.btn {
				width: 250rpx;
			}
		}
	}

	@keyframes fadeIn {
		0% {
			opacity: 0.5;
		}

		100% {
			opacity: 1;
		}
	}

	@keyframes fadeInBig {
		0% {
			opacity: 0;
			transform: scale(0.5);
		}

		100% {
			opacity: 1;
			transform: scale(1);
		}
	}

	@keyframes fadeInUp {
		0% {
			opacity: 0;
			transform: translate3d(0, 100%, 0);
		}

		100% {
			opacity: 1;
			transform: translate3d(0, 0, 0);
		}
	}
</style>